package com.acme.bankapp.networking.client;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.networking.Connection;

import java.io.IOException;
import java.util.Scanner;

public class BankClientStub implements AutoCloseable {
    private Connection connection;

    public BankClientStub(Connection connection) {
        this.connection = connection;
    }

    public String onConnect() throws ClassNotFoundException, IOException {
        return (String) connection.read();
    }

    public String sendReceiveMessageFromConsole() throws ClassNotFoundException, IOException {
        String message = getMessageFromConsole();
        return sendReceiveMessage(message);
    }

    public String sendReceiveMessage(String message) throws ClassNotFoundException, IOException {
        connection.write(message);
        return (String) connection.read();
    }

    public Bank getBank() throws ClassNotFoundException, IOException {
        return (Bank) connection.read();
    }

    private String getMessageFromConsole() {
        System.out.print("write your message to server: ");
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }

    @Override
    public void close() throws IOException, ClassNotFoundException {
        sendReceiveMessage("bye");
    }
}
